package com.jy.wms.webservice;

import com.jy.wms.dao.WmsInventoryPlanMapper;
import com.jy.wms.dao.WmsInventoryTaskMapper;
import com.jy.wms.pojo.WmsInventoryPlan;
import com.jy.wms.pojo.WmsInventoryTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CountryRepository {
    private static final Map<String, Country> countries = new HashMap<>();

    private static final Map<String,String> result=new HashMap<>();
    @Autowired
    private WmsInventoryTaskMapper wmsInventoryTaskMapper;
    @Autowired
    private WmsInventoryPlanMapper wmsInventoryPlanMapper;
    @PostConstruct
    public void initData() {

        Country spain = new Country();
        spain.setName("ssss");
        spain.setCapital("回去sss了");
        spain.setCurrency(Currency.EUR);
        spain.setPopulation(46704314);

        countries.put(spain.getName(), spain);

        Country poland = new Country();
        poland.setName("bbbb");
        poland.setCapital("回去B了");
        poland.setCurrency(Currency.PLN);
        poland.setPopulation(38186860);

        countries.put(poland.getName(), poland);

        Country uk = new Country();
        uk.setName("eeee");
        uk.setCapital("回去E了");
        uk.setCurrency(Currency.GBP);
        uk.setPopulation(63705000);

        countries.put(uk.getName(), uk);
    }

    public String findCountry(String planId,String planNO,String goodsnumber,String locationName,String planNum,String InventoryNum,String differenceState) {
        Assert.notNull(planId, "The country's planId must not be null");
        Assert.notNull(planNO, "The country's planNO must not be null");
        Assert.notNull(goodsnumber, "The country's goodsnumber must not be null");
        Assert.notNull(locationName, "The country's locationName must not be null");
        Assert.notNull(planNum, "The country's planNum must not be null");
        Assert.notNull(InventoryNum, "The country's InventoryNum must not be null");
        Assert.notNull(differenceState, "The country's differenceState must not be null");

        List<WmsInventoryTask> taskList= wmsInventoryTaskMapper.seachByKwAndHpAndPlanId(planId,goodsnumber,locationName);
        if("0".equals(differenceState)&&Integer.parseInt(planNum)==Integer.parseInt(InventoryNum)){
            //无差异
            for(WmsInventoryTask task:taskList){
                task.setPdsl(task.getJhsl());
                task.setCyzt("1");
            }
            wmsInventoryTaskMapper.baseUpdateByPrimaryKeySelective(taskList);

        }else if("1".equals(differenceState)&&Integer.parseInt(planNum)!=Integer.parseInt(InventoryNum)){
            //有差异
            for(WmsInventoryTask task:taskList){
                task.setPdsl(new BigDecimal(0));
                task.setCyzt("2");
            }
            wmsInventoryTaskMapper.baseUpdateByPrimaryKeySelective(taskList);
        }else{
            result.put("error","数据异常，检查后再操作");
        }
        Integer count=wmsInventoryTaskMapper.cyztByplanId(planId);
        if(count>0){
            WmsInventoryPlan plan=new WmsInventoryPlan();
            plan.setId(planId);
            plan.setCyzt("2");
            wmsInventoryPlanMapper.baseUpdateByPrimaryKeySelective(plan);
        }
        result.put("success","盘点此记录成功");
        if(result.containsKey("error")){
            return result.get("error");
        }else{
            return result.get("success");
        }
    }
}
